// pages/Order/OrderAddress/OrderAddress.js
var wareId
var activityId
var tag
var url = wx.getStorageSync('url') 
var shopid = wx.getStorageSync('shopid')
var userId = wx.getStorageSync('userId')
var openid = wx.getStorageSync('openid')
var time_ss = wx.getStorageSync('time_ss')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    address:'0',
    userName:'',
    countDownList: [],
    actEndTimeList: [],
    value:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var shopid = wx.getStorageSync('shopid')
    var userId = wx.getStorageSync('userId')
    var openid = wx.getStorageSync('openid')
    var time_ss = wx.getStorageSync('time_ss')
    console.log(options)
    console.log(userId)
    activityId = options.activityId
    wareId = options.wareId
    tag = options.tag
    var that = this
    wx.showLoading({
      title: '加载中',
    })
    // 获取商品信息
    wx.request({
      url: url +'gateWare/mpInitOrder',
      data: {
        "platform": 2,
        "privId": "0000",
        "resId": "0000",
        "sendTime": time_ss,
        "seq": "232312313",
        "shopId": shopid,
        "source": 1,
        "sysVer": "0.0.1",
        "type": 1,
        "userId": userId,
        "userType": 1,
        "content": {
          "activityId": activityId,
          "wareId": wareId,
          "userId": userId,
          "tag": tag
        }
      },
      method: "POST",
      header: { "Content-Type": "application/JSON" },
      success: function (e) {
        console.log(e)
        wx.hideLoading()
        var data = e.data.content
        var actEndTime = e.data.content.actEndTime
        console.log(data)
        let goodsList = [
          { actEndTime: actEndTime },
        ]
        let endTimeList = [];
        // 将活动的结束时间参数提成一个单独的数组，方便操作
        goodsList.forEach(o => { endTimeList.push(o.actEndTime) })
        that.setData({ actEndTimeList: endTimeList });
        // 执行倒计时函数
        that.countDown();
        that.setData({
          banler: data
        });

      }
    })
  },
  address:function(){
    var that = this
    wx.chooseAddress({
      success(res) {
        that.setData({
          address:'1',
          userName: res.userName,
          postalCode: res.postalCode,
          provinceName: res.provinceName,
          cityName: res.cityName,
          countyName: res.countyName,
          detailInfo: res.detailInfo,
          telNumber: res.telNumber
        })
        // console.log(res.userName)
        // console.log(res.postalCode)
        // console.log(res.provinceName)
        // console.log(res.cityName)
        // console.log(res.countyName)
        // console.log(res.detailInfo)
        // console.log(res.nationalCode)
        // console.log(res.telNumber)
      }
    })
  },
  place_order:function(e){
    var shopid = wx.getStorageSync('shopid')
    var userId = wx.getStorageSync('userId')
    var openid = wx.getStorageSync('openid')
    var time_ss = wx.getStorageSync('time_ss')
    console.log(this.data)
    console.log(wareId)
    var detail = this.data.provinceName + this.data.cityName + this.data.countyName + this.data.detailInfo + this.data.postalCode || null
    console.log(detail)
    var i = this.data.userName;
    if(i==''){
        wx.showToast({
          title: '请添加地址信息',
          icon: 'loading',
          duration: 1000
        })
      } else{
        
      wx.request({
        url: url+'gateOrder/createOrder',
        data: {
          "list": false,
          "nodeId": "kdslfw",
          "platform": 2,
          "privId": "default",
          "resId": "default",
          "sendTime": time_ss,
          "seq": 1,
          "shopId": shopid,
          "source": 11,
          "sysVer": "1.0.0",
          "token": "kdsjfoewi12ijsldks",
          "type": 1,
          "userId": userId,
          "userType": 1,
          "content": {
            "shopId": shopid,
            "serial": "交易流水",
            "status": 2,
            "sales": "促销详情",
            "ticket": "优惠劵使用详情",
            "activityType": tag,
            "activityId": activityId,
            "gift": "赠品详情",
            "distributionPrice": 0,
            "distribution": "包邮",
            "deductionPrice": this.data.banler.yzPrice,
            "payPrice": this.data.banler.orderPrice,
            "totalPrice": this.data.banler.warePrice,
            "receipt": {
              "title": "发票抬头",
              "company": "company",
              "taxNum": "纳税人识别号",
              "currencyType": 1,
              "extInfo": this.data.value
            },
            "address": {
              "name": this.data.userName,
              "phone": this.data.telNumber,
              "detail": detail
            },
            "wareInfos": [{
              "wareId": wareId,
              "wareNum": 1,
              "wareName": this.data.banler.name,
              "unit": "只",
              "price": this.data.banler.orderPrice,
              "wareType": "商品型号",
              "img": this.data.banler.headImg
            }]
          }
        },
        method: "POST",
        header: { "Content-Type": "application/json" },
        success: function (e) {
          console.log(e.data.content.id)
          let order_id = e.data.content.id
          wx.navigateTo({
            url: 'Orderdetails/Orderdetails?order_id=' + order_id,
          })
        }
      })
      }
    
  },
  timeFormat(param) {//小于10的格式化函数
    return param < 10 ? '0' + param : param;
  },
  countDown() {//倒计时函数
    // 获取当前时间，同时得到活动结束时间数组
    let newTime = new Date().getTime();
    let endTimeList = this.data.actEndTimeList;
    let countDownArr = [];

    // 对结束时间进行处理渲染到页面
    endTimeList.forEach(o => {
      let endTime = new Date(o).getTime();
      let obj = null;
      // 如果活动未结束，对时间进行处理
      if (endTime - newTime > 0) {
        let time = (endTime - newTime) / 1000;
        // 获取天、时、分、秒
        let day = parseInt(time / (60 * 60 * 24));
        let hou = parseInt(time % (60 * 60 * 24) / 3600);
        let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
        let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
        obj = {
          day: this.timeFormat(day),
          hou: this.timeFormat(hou),
          min: this.timeFormat(min),
          sec: this.timeFormat(sec)
        }
      } else {//活动已结束，全部设置为'00'
        obj = {
          day: '00',
          hou: '00',
          min: '00',
          sec: '00'
        }
      }
      countDownArr.push(obj);
    })
    // 渲染，然后每隔一秒执行一次倒计时函数
    // console.log(countDownArr)
    // console.log(countDownArr[1])
    this.setData({
      times1: countDownArr[0],

    })
    setTimeout(this.countDown, 1000);

  },
  input_value:function(e){
    console.log(e.detail.value)
    var that = this ;
    that.setData({
      value: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})